package com.thirtydays.frame.ui.recycle

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.view.View
import androidx.annotation.ColorInt
import androidx.annotation.Nullable
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.SizeUtils


/**
 * @author 小垚
 * @创建时间： 2020/12/17
 * @描述：列表分割线
 **/
abstract class DividerItemDecoration : RecyclerView.ItemDecoration {
    lateinit var mPaint: Paint

    lateinit var context: Context

    constructor(context: Context) : super() {
        this.context = context
        mPaint = Paint(Paint.ANTI_ALIAS_FLAG)
        mPaint.style = Paint.Style.FILL
    }

    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        //left, top, right, bottom
        val childCount = parent.childCount
        for (i in 0 until childCount) {
            val child: View = parent.getChildAt(i)
            val itemPosition = (child.layoutParams as RecyclerView.LayoutParams).viewLayoutPosition
            val divider: Divider = getDivider(itemPosition, state.itemCount)

            divider.mLeftSideLine?.let {
                if (it.isHave) {
                    val lineWidthPx: Int = SizeUtils.dp2px(it.widthDp)
                    val startPaddingPx: Int = SizeUtils.dp2px(it.startPaddingDp)
                    val endPaddingPx: Int = SizeUtils.dp2px(it.endPaddingDp)
                    drawChildLeftVertical(child, c, parent, it.color, lineWidthPx, startPaddingPx, endPaddingPx)
                }
            }

            divider.mTopSideLine?.let {
                if (it.isHave) {
                    val lineWidthPx: Int = SizeUtils.dp2px(it.widthDp)
                    val startPaddingPx: Int = SizeUtils.dp2px(it.startPaddingDp)
                    val endPaddingPx: Int = SizeUtils.dp2px(it.endPaddingDp)
                    drawChildTopHorizontal(child, c, parent, it.color, lineWidthPx, startPaddingPx, endPaddingPx)
                }
            }

            divider.mRightSideLine?.let {
                if (it.isHave) {
                    val lineWidthPx: Int = SizeUtils.dp2px(it.widthDp)
                    val startPaddingPx: Int = SizeUtils.dp2px(it.startPaddingDp)
                    val endPaddingPx: Int = SizeUtils.dp2px(it.endPaddingDp)
                    drawChildRightVertical(child, c, parent, it.color, lineWidthPx, startPaddingPx, endPaddingPx)
                }
            }

            divider.mBottomSideLine?.let {
                if (it.isHave) {
                    val lineWidthPx: Int = SizeUtils.dp2px(it.widthDp)
                    val startPaddingPx: Int = SizeUtils.dp2px(it.startPaddingDp)
                    val endPaddingPx: Int = SizeUtils.dp2px(it.endPaddingDp)
                    drawChildBottomHorizontal(child, c, parent, it.color, lineWidthPx, startPaddingPx, endPaddingPx)
                }
            }
        }
    }


    open fun drawChildBottomHorizontal(
        child: View,
        c: Canvas,
        parent: RecyclerView,
        @ColorInt color: Int,
        lineWidthPx: Int,
        startPaddingPx: Int,
        endPaddingPx: Int
    ) {
        var leftPadding = 0
        var rightPadding = 0
        leftPadding = if (startPaddingPx <= 0) {
            //padding<0当作==0处理
            //上下左右默认分割线的两头都出头一个分割线的宽度，避免十字交叉的时候，交叉点是空白
            -lineWidthPx
        } else {
            startPaddingPx
        }
        rightPadding = if (endPaddingPx <= 0) {
            lineWidthPx
        } else {
            -endPaddingPx
        }
        val params = child.layoutParams as RecyclerView.LayoutParams
        val left: Int = child.left - params.leftMargin + leftPadding
        val right: Int = child.right + params.rightMargin + rightPadding
        val top: Int = child.bottom + params.bottomMargin
        val bottom = top + lineWidthPx
        mPaint.color = color
        c.drawRect(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat(), mPaint)
    }

    open fun drawChildTopHorizontal(
        child: View,
        c: Canvas,
        parent: RecyclerView,
        @ColorInt color: Int,
        lineWidthPx: Int,
        startPaddingPx: Int,
        endPaddingPx: Int
    ) {
        var leftPadding = 0
        var rightPadding = 0
        leftPadding = if (startPaddingPx <= 0) {
            //padding<0当作==0处理
            //上下左右默认分割线的两头都出头一个分割线的宽度，避免十字交叉的时候，交叉点是空白
            -lineWidthPx
        } else {
            startPaddingPx
        }
        rightPadding = if (endPaddingPx <= 0) {
            lineWidthPx
        } else {
            -endPaddingPx
        }
        val params = child.layoutParams as RecyclerView.LayoutParams
        val left = child.left - params.leftMargin + leftPadding
        val right = child.right + params.rightMargin + rightPadding
        val bottom = child.top - params.topMargin
        val top = bottom - lineWidthPx
        mPaint.color = color
        c.drawRect(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat(), mPaint)
    }

    open fun drawChildLeftVertical(
        child: View,
        c: Canvas,
        parent: RecyclerView,
        @ColorInt color: Int,
        lineWidthPx: Int,
        startPaddingPx: Int,
        endPaddingPx: Int
    ) {
        var topPadding = 0
        var bottomPadding = 0
        topPadding = if (startPaddingPx <= 0) {
            //padding<0当作==0处理
            //上下左右默认分割线的两头都出头一个分割线的宽度，避免十字交叉的时候，交叉点是空白
            -lineWidthPx
        } else {
            startPaddingPx
        }
        bottomPadding = if (endPaddingPx <= 0) {
            lineWidthPx
        } else {
            -endPaddingPx
        }
        val params = child.layoutParams as RecyclerView.LayoutParams
        val top = child.top - params.topMargin + topPadding
        val bottom = child.bottom + params.bottomMargin + bottomPadding
        val right = child.left - params.leftMargin
        val left = right - lineWidthPx
        mPaint.color = color
        c.drawRect(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat(), mPaint)
    }

    open fun drawChildRightVertical(
        child: View,
        c: Canvas,
        parent: RecyclerView,
        @ColorInt color: Int,
        lineWidthPx: Int,
        startPaddingPx: Int,
        endPaddingPx: Int
    ) {
        var topPadding = 0
        var bottomPadding = 0
        topPadding = if (startPaddingPx <= 0) {
            //padding<0当作==0处理
            //上下左右默认分割线的两头都出头一个分割线的宽度，避免十字交叉的时候，交叉点是空白
            -lineWidthPx
        } else {
            startPaddingPx
        }
        bottomPadding = if (endPaddingPx <= 0) {
            lineWidthPx
        } else {
            -endPaddingPx
        }
        val params = child.layoutParams as RecyclerView.LayoutParams
        val top = child.top - params.topMargin + topPadding
        val bottom = child.bottom + params.bottomMargin + bottomPadding
        val left = child.right + params.rightMargin
        val right = left + lineWidthPx
        mPaint.color = color
        c.drawRect(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat(), mPaint)
    }

    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {

        //outRect 看源码可知这里只是把Rect类型的outRect作为一个封装了left,right,top,bottom的数据结构,
        //作为传递left,right,top,bottom的偏移值来用的
        val itemPosition = (view.layoutParams as RecyclerView.LayoutParams).viewLayoutPosition
        var divider: Divider = getDivider(itemPosition, state.itemCount)
        if (divider == null) {
            divider = DividerBuilder().create()
        }
        var left = 0
        var top = 0
        var right = 0
        var bottom = 0
        divider.mLeftSideLine?.let {
            if (it.isHave) {
                left = SizeUtils.dp2px(it.widthDp)
            }
        }

        divider.mTopSideLine?.let {
            if (it.isHave) {
                top = SizeUtils.dp2px(it.widthDp)
            }
        }

        divider.mRightSideLine?.let {
            if (it.isHave) {
                right = SizeUtils.dp2px(it.widthDp)
            }
        }

        divider.mBottomSideLine?.let {
            if (it.isHave) {
                bottom = SizeUtils.dp2px(it.widthDp)
            }
        }

        outRect.set(left, top, right, bottom)
    }

    @Nullable
    abstract fun getDivider(itemPosition: Int, itemCount: Int): Divider

}